﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Steamrunner
{
    class PlayerManager : GameComponent
    {
        Dictionary<string, Player> players = new Dictionary<string, Player>();

        public PlayerManager(Game game)
            : base(game)
        {
        }

        public override void Update(GameTime gameTime)
        {
            foreach (KeyValuePair<string, Player> player in players)
            {
                player.Value.Update(gameTime);
            }
            
            base.Update(gameTime);
        }

        public void DrawPlayers(GameTime gameTime)
        {
            foreach (KeyValuePair<string, Player> player in players)
            {
                player.Value.Draw(gameTime);
            }
        }

        public void DrawPlayer(GameTime gameTime, string id)
        {
            players[id].Draw(gameTime);
        }

        public void AddPlayer(string id, Player player)
        {
            players.Add(id, player);
        }

        public List<Vector2> GetPlayerPositions()
        {
            List<Vector2> positions = new List<Vector2>();
            
            foreach (KeyValuePair<string, Player> player in players)
            {
                positions.Add(player.Value.Position);
            }

            return positions;
        }

        public Vector2 GetPlayerPosition(int index)
        {
            if (index == (int)Enums.PlayerColor.BLUE)
            {
                return players["blue"].Position;
            }
            else if (index == (int)Enums.PlayerColor.GREEN)
            {
                return players["green"].Position;
            }
            else if (index == (int)Enums.PlayerColor.RED)
            {
                return players["red"].Position;
            }
            else if (index == (int)Enums.PlayerColor.YELLOW)
            {
                return players["yellow"].Position;
            }

            return Vector2.Zero;
        }

        public void RemovePlayer(string id)
        {
            Player player = players[id];

            if (player != null)
            {
                player.Dispose();
                players.Remove(id);
            }
        }

        public Player GetPlayer(int index)
        {
            if (index == (int)Enums.PlayerColor.BLUE)
            {
                return players["blue"];
            }
            else if (index == (int)Enums.PlayerColor.GREEN)
            {
                return players["green"];
            }
            else if (index == (int)Enums.PlayerColor.RED)
            {
                return players["red"];
            }
            else if (index == (int)Enums.PlayerColor.YELLOW)
            {
                return players["yellow"];
            }

            return null;
        }
    }
}
